package com.tebiecloud.mpweixin.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.mpweixin.client.model.entity.MpweixinUserBankCard;
import com.tebiecloud.mpweixin.client.model.vo.UserBankCardVO;
import com.tebiecloud.mpweixin.server.mapper.MpweixinUserBankCardMapper;
import com.tebiecloud.mpweixin.server.service.MpweixinUserBankCardService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户绑定的银行卡 服务实现类
 * @author tebie
 * @date 2019-09-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MpweixinUserBankCardServiceImpl extends BaseServiceImpl<MpweixinUserBankCardMapper, MpweixinUserBankCard> implements MpweixinUserBankCardService {

    @Autowired
    MpweixinUserBankCardMapper mpweixinUserBankCardMapper;

    @Override
    public UserBankCardVO info(Long userId) {
        UserBankCardVO userBankCardVO = new UserBankCardVO();
        //查询用户有无绑定银行卡
        QueryWrapper<MpweixinUserBankCard> mpweixinUserBankCardQueryWrapper = new QueryWrapper<MpweixinUserBankCard>();
        mpweixinUserBankCardQueryWrapper.eq("user_id",userId);
        mpweixinUserBankCardQueryWrapper.eq("status",1);
        MpweixinUserBankCard mpweixinUserBankCard = mpweixinUserBankCardMapper.selectOne(mpweixinUserBankCardQueryWrapper);
        if(mpweixinUserBankCard!=null){
            String identityNo = mpweixinUserBankCard.getIdentityNo();
            if(StringUtils.isNotEmpty(identityNo) && identityNo.length()>=15){
                Integer length = identityNo.length()-7;
                String repalcement = "$1";
                for(int i=0;i<length;i++){
                    repalcement = repalcement + "*";
                }
                repalcement = repalcement + "$2";
                String regex = "(\\d{3})\\d{"+length+"}(\\d{4})";
                userBankCardVO.setIdentityNo(identityNo.replaceAll(regex,repalcement));
            }else{
                userBankCardVO.setIdentityNo(identityNo);
            }
            String cardNo = mpweixinUserBankCard.getCardNo();
            if(StringUtils.isNotEmpty(cardNo)){
                Integer length = cardNo.length()-8;
                String repalcement = "$1";
                for(int i=0;i<length;i++){
                    repalcement = repalcement + "*";
                }
                repalcement = repalcement + "$2";
                String regex = "(\\d{4})\\d{"+length+"}(\\d{4})";
                userBankCardVO.setCardNo(cardNo.replaceAll(regex,repalcement));
            }else{
                userBankCardVO.setCardNo(cardNo);
            }
            userBankCardVO.setName(mpweixinUserBankCard.getName());
            userBankCardVO.setPhone(mpweixinUserBankCard.getPhone());
            userBankCardVO.setBank(mpweixinUserBankCard.getBank());
            userBankCardVO.setIcon(mpweixinUserBankCard.getIcon());
            userBankCardVO.setType(mpweixinUserBankCard.getType());
        }
        return userBankCardVO;
    }
}
